package com.tc.have_a_tired.ui;

import javax.swing.*;
import java.awt.*;

/**
 * 提醒窗口类
 */
public class ReminderWindow extends JDialog {

    private final Runnable onAcknowledged;

    public ReminderWindow(String message, int restDuration, Runnable onAcknowledged) {
        this.onAcknowledged = onAcknowledged;
        initUI(message, restDuration);
    }

    private void initUI(String message, int restDuration) {
        setTitle("休息提醒");
        setModal(true);
        setAlwaysOnTop(true);
        setResizable(false);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        // 设置窗口大小和位置
        setSize(450, 300);
        centerWindow();

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(15, 15));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        mainPanel.setBackground(Color.WHITE);

        // 标题
        JLabel titleLabel = new JLabel("休息提醒", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
        titleLabel.setForeground(new Color(51, 51, 51));
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        // 提示语内容
        JTextArea messageArea = new JTextArea(message);
        messageArea.setEditable(false);
        messageArea.setOpaque(false);
        messageArea.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        messageArea.setForeground(new Color(68, 68, 68));
        messageArea.setLineWrap(true);
        messageArea.setWrapStyleWord(true);
        messageArea.setAlignmentX(Component.CENTER_ALIGNMENT);

        // 将文本区域放在滚动面板中
        JScrollPane scrollPane = new JScrollPane(messageArea);
        scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setPreferredSize(new Dimension(400, 150));

        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
        buttonPanel.setOpaque(false);

        ColoredButton acknowledgeButton = new ColoredButton(
                "知道了",
                new Color(70, 130, 180),
                Color.WHITE,
                new Color(100, 149, 237));
        acknowledgeButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        acknowledgeButton.setPreferredSize(new Dimension(120, 35));
        acknowledgeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        acknowledgeButton.addActionListener(e -> {
            if (onAcknowledged != null) {
                onAcknowledged.run();
            }
            dispose();
        });

        buttonPanel.add(acknowledgeButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);

        // 设置窗口关闭时的行为
        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                if (onAcknowledged != null) {
                    onAcknowledged.run();
                }
            }
        });
    }

    /**
     * 将窗口居中显示
     */
    private void centerWindow() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - getWidth()) / 2;
        int y = (screenSize.height - getHeight()) / 2;
        setLocation(x, y);
    }
}
